<?php

// 提现
class CashAction extends SecurityAction {
	function _initialize() {
		$this->assign ( "menuFirst", session ( 'menuFirst' ) );
		$this->assign ( "menuSecond", session ( 'menuSecond' ) );
	}
	
	// 提取现金
	function draw() {
		$ob = M ( "csl_user" );
		$info = $ob->where ( array (
				'uid' => session ( 'info.uid' ) 
		) )->find ();
		
		if ($info ['bank'] == '' || $info ['cardname'] == '' || $info ['cardno'] == '') {
			$this->redirect ( "/Admin/Cash/bank" );
		}
		
		// 生成一条提取记录并扣减余额
		if ($info ['money'] > 0 && $_POST ['money'] > 0 && $info ['money'] >= $_POST ['money']) {
			$data ['uid'] = session ( 'info.uid' );
			$data ['bank'] = $info ['bank'];
			$data ['cardname'] = $info ['cardname'];
			$data ['cardno'] = $info ['cardno'];
			$data ['money'] = $_POST ['money'];
			$data ['ctime'] = date ( 'Y-m-d H:i:s' );
			$data ['status'] = 0;
			M ( "csl_cash" )->add ( $data );
			
			$yue = $info ['money'] - $_POST ['money'];
			$ob->where ( array (
					'uid' => session ( 'info.uid' ) 
			) )->setField ( 'money', $yue );
			
			//提现日志
			LogTransactionModel::ModifyMoney($info['uid'],'','提现',$info['money'],(0-$_POST ['money']),$yue,'申请提现  - 等待中');
			
			
			$this->redirect ( "/Admin/Console/index" );
		}
		
		$this->assign ( 'info', $info );
		$this->display ( "Cash/draw" );
	}
	
	// 修改银行卡资料
	function bank() {
		$ob = M ( "csl_user" );
		$info = $ob->where ( array (
				'uid' => session ( 'info.uid' ) 
		) )->find ();
		
		if ($_POST) {
			$data ['bank'] = trim ( $_POST ['bank'] );
			$data ['cardname'] = trim ( $_POST ['cardname'] );
			$data ['cardno'] = trim ( $_POST ['cardno'] );
			$ob->where ( array (
					'uid' => session ( 'info.uid' ) 
			) )->save ( $data );
			
			$this->redirect ( "/Admin/Console/index" );
		}
		
		$this->assign ( 'info', $info );
		$this->display ( "Cash/bank" );
	}
	
	// 处理提现列表页
	function audit() {
		// 超级管理员权限
		if (session ( "info.rid" ) != '1' && session ( "info.rid" ) != '10') {
			$this->redirect ( '/Admin/Console/index' );
			return;
		}
		
		$om = M("log_money");
		$ob = D ( "Cash" );
		$map = array ();
		
		$cpp = 10; // 分页数
		import ( 'ORG.Util.Page' ); // 导入分页类
		$count = $ob->where ( $map )->count (); // 查询满足要求的总记录数
		$Page = new Page ( $count, $cpp ); // 实例化分页类 传入总记录数和每页显示的记录数
		$show = $Page->show (); // 分页显示输出
		$xuhao = $Page->firstRow;
		
		$list = $ob->where ( $map )->order ( 'id desc' )->limit ( $Page->firstRow . ',' . $Page->listRows )->getlist ();
		
		foreach($list as $key=>$value){
			if($value['status'] == '0'){
				$list[$key]['cash_history'] = $om->where(['cid'=>$value['cid'],'uid'=>$value['uid']])->order('id desc')->limit(5)->select();
			}
		}
		$this->assign ( "list", $list );
		$this->assign ( 'show', $show ); // 赋值分页输出
		$this->assign ( "xuhao", $xuhao );
		$this->display ( "Cash/audit" );
	}
	
	// 提现成功--处理状态
	function dispose() {
		// 超级管理员权限
		if (session ( "info.rid" ) != '1' && session ( "info.rid" ) != '10') {
			$this->redirect ( '/Admin/Console/index' );
			return;
		}
		
		$ob = D ( "Cash" );
		$map ['id'] = $_GET ['id'];
		$result = $ob->where ( $map )->find ();
		
		if (! empty ( $result )) {
			if ($result ['status'] == 0) {
				$data = array ();
				$data ['status'] = 1;
				$data ['dtime'] = date ( 'Y-m-d H:i:s' );
				$ob->where ( $map )->save ( $data );
				CommonFunc::Log ( "{$result[cardname]}[{$result[bank]}:{$result[cardno]}] 提现[RMB:{$result[money]}]成功。操作人：" . session ( "info.name" ) . "[" . session ( "info.username" ) . "]", '提现' );
				
				if ($result ['uid'] != 0) {
					$db = M ( "csl_user" );
					$draw = array ();
					$draw ['uid'] = $result ['uid'];
					$info = $db->where ( $draw )->find ();
					LogTransactionModel::ModifyMoney($info['uid'],'','提现',$info['money'],0,$info['money'],"提现审核成功 - 提现金额{$result[money]}元");
				} elseif ($result ['cid'] != 0) {
					$db = M ( "csl_customer" );
					$draw = array ();
					$draw ['cid'] = $result ['cid'];
					$info = $db->where ( $draw )->find ();
					LogTransactionModel::ModifyMoney('',$info['cid'],'提现',$info['money'],0,$info['money'],"提现审核成功 - 提现金额{$result[money]}元");
				}
				
			}
			
			$this->redirect ( "/Admin/Cash/audit/p/" . $_GET ['p'] );
		}
	}
	
	// 提现失败
	function checkback() {
		// 超级管理员权限
		if (session ( "info.rid" ) != '1' && session ( "info.rid" ) != '10') {
			$this->redirect ( '/Admin/Console/index' );
			return;
		}
		
		// 提现失败原因
		if ($_POST ['note'] != '') {
			$ob = D ( "Cash" );
			$map ['id'] = $_GET ['id'];
			$result = $ob->where ( $map )->find ();
			
			if (! empty ( $result )) {
				if ($result ['status'] == 0) {
					$data = array ();
					$data ['status'] = 2;
					$data ['note'] = $_POST ['note'];
					$data ['dtime'] = date ( 'Y-m-d H:i:s' );
					$ob->where ( $map )->save ( $data );
					
					// 将该笔金额退还原账户
					if ($result ['uid'] != 0) {
						$db = M ( "csl_user" );
						$draw = array ();
						$draw ['uid'] = $result ['uid'];
						$info = $db->where ( $draw )->find ();
						
						$money = $info ['money'];
						$money += $result ['money'];
						$db->where ( $draw )->setField ( 'money', $money );
						
						//提现日志
						LogTransactionModel::ModifyMoney($result ['uid'],'','提现',$info ['money'],$result ['money'],$money,'提现失败');
						
					} elseif ($result ['cid'] != 0) {
						$db = M ( "csl_customer" );
						$draw = array ();
						$draw ['cid'] = $result ['cid'];
						$info = $db->where ( $draw )->find ();
						
						$money = $info ['money'];
						$money += $result ['money'];
						$db->where ( $draw )->setField ( 'money', $money );
						
						//提现日志
						LogTransactionModel::ModifyMoney('',$result ['cid'],'提现',$info ['money'],$result ['money'],$money,'提现失败');
					}
					
					CommonFunc::Log ( "{$result[cardname]}[{$result[bank]}:{$result[cardno]}] 提现[RMB:{$result[money]}]失败，金额返回原账户。操作人：" . session ( "info.name" ) . "[" . session ( "info.username" ) . "]", '提现' );
					
					
				}
				
				$this->redirect ( "/Admin/Cash/audit/p/" . $_GET ['p'] );
			}
		}
		
		$this->display ( "Cash/checkback" );
	}
}

?>